package com.yfree.utils;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.webkit.JavascriptInterface;

import com.yfree.YConfig;
import com.yfree.activities.YActivity;
import com.yfree.activities.YWebActivity;
import com.yfree.activities.YWebActivity2;
import com.yfree.views.YProgressDialog;
import com.yfree.views.YWebView2;

public class YJSDelegate2 {
    public static final String instance = "yPhone";
    public YActivity yActivity;
    public YWebView2 webView;

    public YJSDelegate2(YActivity yActivity, YWebView2 webView) {
        this.yActivity = yActivity;
        this.webView = webView;
    }

    @JavascriptInterface
    public String getDevice() {
        return YConfig.device;
    }

    @JavascriptInterface
    public String getAppName() {
        return YAppUtils.getAppName(yActivity);
    }

    @JavascriptInterface
    public String getVersion() {
        return String.valueOf(YVersionUtils.getVersionCode(yActivity));
    }

    @JavascriptInterface
    public String getIP() {
        return YConfig.IP;
    }

    @JavascriptInterface
    public String getDebug() {
        return YConfig.DEBUG ? "1" : "0";
    }

    @JavascriptInterface
    public String getHttpHeader() {
        return "{}";
    }

    @JavascriptInterface
    public String getUserEntity() {
        return "";
    }

    @JavascriptInterface
    public void closeKeyBoard() {
        yActivity.yCloseInputMethod();
    }

    @JavascriptInterface
    public void openQQ(String qq) {
        YQQUtils.openQQ(yActivity, qq);
    }

    @JavascriptInterface
    public void goBack() {
        webView.goBack();
    }

    @JavascriptInterface
    public void close() {
        yActivity.finish();
    }

    @JavascriptInterface
    public void close2() {
        yActivity.finish();
    }

    @JavascriptInterface
    public void openBrowser(String url) {
        yActivity.startActivity(new Intent().setAction("android.intent.action.VIEW").setData(Uri.parse(url)));
    }

    @JavascriptInterface
    public void log(String msg) {
        yActivity.yLog(msg);
    }

    @JavascriptInterface
    public void toast(String msg) {
        yActivity.yToast(msg.replace("<br/>", "\n"));
    }

    @JavascriptInterface
    public void showProgress() {
        YProgressDialog.showProgress(yActivity);
    }

    @JavascriptInterface
    public void dismissProgress() {
        YProgressDialog.dismissProgress(yActivity);
    }

    @JavascriptInterface
    public void copy(String content) {
        ((ClipboardManager) yActivity.context.getSystemService(Context.CLIPBOARD_SERVICE)).setPrimaryClip(ClipData.newPlainText("text", content));
    }

    // self
    public void yEvaluateScript(String script) {
        if (yActivity instanceof YWebActivity) ((YWebActivity) yActivity).yEvaluateScript(script);
        else if (yActivity instanceof YWebActivity2) ((YWebActivity2) yActivity).yEvaluateScript(script);
    }
}